#include "systemtray.h"
#include <QApplication>
#include <QDebug>


SystemTray::SystemTray(QObject *parent) : QObject(parent),
    icon(QIcon(":/image/alarm.png")) {
    if(!QSystemTrayIcon::isSystemTrayAvailable()){
        qWarning() << "No system tray available!";
        return;
    }

    trayIcon = new QSystemTrayIcon(icon, this);
    trayIcon->setVisible(true);
    trayIcon->setToolTip("智能闹钟");

    connect(trayIcon, &QSystemTrayIcon::activated, this, &SystemTray::systemTrayIconActivated);

    contextMenu = new QMenu();
    quitAction = contextMenu->addAction("退出");
    connect(quitAction, &QAction::triggered, qApp, &QApplication::quit);

    trayIcon->setContextMenu(contextMenu);
}

void SystemTray::showMessage(const QString &title, const QString &message)
{
    trayIcon->showMessage(title, message, icon, 30000);
}

void SystemTray::showMessage(const QString &title, const QString &message, const int& msecs)
{
    trayIcon->showMessage(title, message, icon, msecs);
}

bool SystemTray::isVisible()
{
    if(trayIcon)
    {
        return trayIcon->isVisible();
    }
    return false;
}
